package com.bw.module_car.viewmodel

import androidx.lifecycle.MutableLiveData
import com.bw.lib_common.coroutine.CoroutineHelper
import com.bw.lib_common.vm.BaseViewModel2
import com.bw.module_car.entity.CarEntity
import com.bw.module_car.entity.ProvincesEntity
import com.bw.module_car.entity.RecordEntity
import com.bw.module_car.repo.CarRepo
import kotlinx.coroutines.launch

class CarViewModel : BaseViewModel2<CarRepo>() {
    val getCarSuccess by lazy { MutableLiveData<List<CarEntity>>() }

    fun getCar(){
        CoroutineHelper.mViewModelScope.launch(CoroutineHelper.mDispatcher) {
            val result = mRepository.getCar()
            if(result.code==200){
                getCarSuccess.postValue(result.data)
            }else{
                mErrorLiveData.postValue("获取数据异常！！")
            }
        }
    }

    val getProvincesSuccess by lazy { MutableLiveData<List<ProvincesEntity>>() }
    fun getProvinces(){
        CoroutineHelper.mViewModelScope.launch(CoroutineHelper.mDispatcher) {
            val result = mRepository.getProvinces()
            if(result.code==200){
                getProvincesSuccess.postValue(result.data)
            }else{
                mErrorLiveData.postValue("获取数据异常！！")
            }
        }
    }

    val getRecordSuccess by lazy { MutableLiveData<List<RecordEntity>>() }
    fun getRecord(){
        CoroutineHelper.mViewModelScope.launch(CoroutineHelper.mDispatcher) {
            val result = mRepository.getRecord()
            if(result.code==200){
                getRecordSuccess.postValue(result.data)
            }else{
                mErrorLiveData.postValue("获取数据异常！！")
            }
        }
    }

    override fun createRepository(): CarRepo = CarRepo()
}